using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;

using CNCSoft.EWinform;
using CNCSoft.EWinform.ControlUtils;
//using CNCSoft.EWinform.ERPApps;
using CNCSoft.EWinform.AGrid;
using CNCSoft.EData;

using Common;

namespace CNCSoft.ErpApps.TMS {

	/// <summary>
	/// Summary description for DongYBaoLuuData
	/// </summary>
	public class DongYBaoLuuData: TMSDataBase  {

		public enum TL  {
            BaoLuu,
        }

        private string sqlBaoLuu =
            "SELECT	ID, Loai, [EnrollNumber], [Ngay], [LyDoBaoLuu], [Lop], SoLanFeeplanBatDauChuyen, UpdateUserID, UpdateDate FROM [BaoLuu] ";

		public DongYBaoLuuData() {

            ds.Tables.Add("BaoLuu");

			initData("('')", true);
		}

		public void initData(string strEnrollNumber, bool firstLoading) {
            try
            {
                if (!firstLoading)
                {
                    getTable(TL.BaoLuu).Clear();
                }
                SQLHelper.Fill(getTable(TL.BaoLuu), GlobalVariable.WSInfo.ConnectionString,
                    sqlBaoLuu + " WHERE [EnrollNumber] In " + strEnrollNumber);
             
                if (firstLoading)
                {
                    // Set primary key(s)
                    getTable(TL.BaoLuu).PrimaryKey = new DataColumn[] { getTable(TL.BaoLuu).Columns["ID"] };


                    // Set default values
                    getTable(TL.BaoLuu).Columns["ID"].AutoIncrement = true;
                    getTable(TL.BaoLuu).Columns["ID"].AutoIncrementStep = 1;
                    getTable(TL.BaoLuu).Columns["Loai"].DefaultValue = "BL";
                    getTable(TL.BaoLuu).Columns["EnrollNumber"].DefaultValue = "";
                    getTable(TL.BaoLuu).Columns["Ngay"].DefaultValue = GlobalVariable.WSInfo.WorkingDate;
                    getTable(TL.BaoLuu).Columns["LyDoBaoLuu"].DefaultValue = "";
                    getTable(TL.BaoLuu).Columns["Lop"].DefaultValue = "";
                    getTable(TL.BaoLuu).Columns["SoLanFeeplanBatDauChuyen"].DefaultValue = "0";
                    getTable(TL.BaoLuu).Columns["UpdateUserID"].DefaultValue = GlobalVariable.WSInfo.UserID;
                    getTable(TL.BaoLuu).Columns["UpdateDate"].DefaultValue = DateTime.Now.Date;

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"Init database");
            }
		}

		public bool createNewRow() {
			try {
				DataRow dr = getTable(TL.BaoLuu).NewRow();
                getTable(TL.BaoLuu).Rows.Add(dr);
				return true;
			}
			catch {
				MessageBox.Show(RS.ERROR_CREATE_NEW_ROW, RS.ERROR_CREATE_NEW_ROW_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return false;
		}

		public bool update() {
			try {
                new SimpleMasterUpdater (sqlBaoLuu,  getTable(DongYBaoLuuData.TL.BaoLuu), GlobalVariable.WSInfo.ConnectionString);
                ds.AcceptChanges();
				return true;
			} catch (Exception ex) {
				MessageBox.Show(RS.ERROR_UPDATE +"\n" +ex.Message, RS.ERROR_UPDATE_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return false;
		}

		public DataTable getTable(TL index) {
			return ds.Tables[(int)index];
		}

	}
}
